package com.lankystar.logistucs.spring.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.service.CompanyService;
import com.lankystar.logistucs.spring.service.OrderService;
import com.lankystar.logistucs.spring.service.TruckService;
import com.lankystar.logistucs.spring.service.UserService;

@Controller
@RequestMapping(value = "/company")
public class CompanyController {
/**
 * 运输队
 */
	@Autowired
	private OrderService orderService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private TruckService truckService;
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="lastOrderlist.do")
	public String lastOrderlist(Model model,Integer userId,HttpSession ses){
		List<Map<String, Object>> cargolist = companyService.queryLastOrderAndAddresslist();
		List<Map<String, Object>> trucklist = truckService.queryTrucklist(userId);
		List<User> userlist = userService.queryCanUserlist();
		model.addAttribute("cargolist", cargolist);
		model.addAttribute("trucklist", trucklist);
		model.addAttribute("userlist", userlist);
		User user = (User) ses.getAttribute("userSession");
		model.addAttribute("userId", user.getUserId());
		return "/company/lastOrder";
	}
	
	@RequestMapping(value="toTrucklist.do")
	public String toTrucklist(Model model,Integer userId){
		List<Map<String, Object>> cargolist = companyService.queryCargoAndAddresslist();
		List<Map<String, Object>> trucklist = truckService.queryTrucklist(userId);
		List<User> userlist = userService.queryCanUserlist();
		model.addAttribute("cargolist", cargolist);
		model.addAttribute("trucklist", trucklist);
		model.addAttribute("userlist", userlist);
		return "/company/trucklist";
	}
	/**
	 * 查找运输公司的所有车、车的状态
	 * @return
	 */
	@RequestMapping(value="queryTruckAndTruckStatus.do")
	public String queryTruckAndTruckStatus(Model model,HttpSession ses){
		try {
			User user = (User) ses.getAttribute("userSession");
			List<Map<String, Object>> trucklist = truckService.queryTruckAndStatuslist(user.getUserId());
			model.addAttribute("trucklist", trucklist);
			model.addAttribute("userId", user.getUserId());
		} catch (Exception e) {
			System.out.println("/company/queryTruckAndTruckStatus");
		}
		return "/company/truck";
	}
	
	@RequestMapping(value="createTwoOrder.do")
	public String createTwoOrder(Model model,HttpSession ses,String values,Integer truckId,Integer userId){
		try {
			User user = (User) ses.getAttribute("userSession");
			List<Map<String, Object>> maplist = orderService.addOneOrder(values, truckId, userId, user);
			model.addAttribute("maplist",maplist);
			model.addAttribute("userId", user.getUserId());
		} catch (Exception e) {
			
		}
		return "/order/oneOrder";
	}
}
